package com.zhiyou.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou.entity.Contract;
import com.zhiyou.entity.House;
import com.zhiyou.entity.Lessee;
import com.zhiyou.service.ContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/contract")
public class ContractController {

    @Autowired
    ContractService contractService;

    @RequestMapping("/getAll")
    public String getAll(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model){
        PageHelper.startPage(pn, 5);
        List<Contract> list= contractService.getstatus();
        PageInfo<Contract> page=new PageInfo<>(list,5);
        model.addAttribute("pageinfo",page);
        return "contract";
    }

    @RequestMapping("/getLikeAll")
    public String getLikeAll(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model,@RequestParam(value ="likes")String likes){
        //System.out.println(likes);
        PageHelper.startPage(pn, 5);
        List<Contract> list=contractService.getLikeAll("%"+likes+"%");
        //System.out.println(list.size());
        //System.out.println(list.toString());
        PageInfo<Contract> page=new PageInfo<>(list,5);
        model.addAttribute("pageinfo",page);
        return "contract";
    }
    @RequestMapping("/addcontract")
    public String addcontract(){

        return "addcontract";
    }

    @RequestMapping("/addcontract1")
    public String addcontract1(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model,Contract contract){
        System.out.println(contract.getHId());
        House house=new House();
        house.sethId(contract.getHId());
        contract.setHouse(house);
        List<House> houselist=contractService.selectbyid(contract);
        Lessee lessee=new Lessee();
        lessee.setlId(contract.getLId());
        contract.setLessee(lessee);
        List<Lessee> lesseelist=contractService.selectbylessee(contract);
        System.out.println(houselist.size()+"   "+lesseelist.get(0).toString());
        if (houselist.size()>=1 && lesseelist.size()>=1) {
            Date date = new Date();
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = simpleDateFormat.format(date);
            contract.setCTime(format);
            contract.setCStatus(1);
            //System.out.println(contract.toString());
            int i = contractService.insetcontract(contract);
            if (i >= 1) {
                PageHelper.startPage(pn, 5);
                List<Contract> list = contractService.getstatus();
                PageInfo<Contract> page = new PageInfo<>(list, 5);
                model.addAttribute("pageinfo", page);
            } else {
                return "error";
            }
        }else {
            return "error";
        }

        return "contract";
    }


    @RequestMapping("/delcontract")
    public String delcontract(@RequestParam(value = "cid")Integer Cid,@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model){
        //System.out.println(Cid);
        int f=contractService.delcontract(Cid);
        if (f<1){
            return "error";
        }
        PageHelper.startPage(pn, 5);
        List<Contract> list= contractService.getstatus();
        PageInfo<Contract> page=new PageInfo<>(list,5);
        model.addAttribute("pageinfo",page);
        return "contract";
    }

    @RequestMapping("/updatacontract")
    public String updatacontract(@RequestParam(value = "cid")Integer Cid,@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model){
        //System.out.println("-------------"+Cid);
        Contract contract=new Contract();
        contract.setCId(Cid);
        List<Contract> list=contractService.selectbycid(contract);
        model.addAttribute("list",list);
        //System.out.println("*************"+list.get(0).getLId());
        return "updatacontract";
    }

    @RequestMapping("/updatacontract1")
    public String updatacontract1(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model,Contract contract){
        //System.out.println(contract.toString());
        contract.setCStatus(1);
        int f=contractService.updatacontract(contract);
       // System.out.println(f);
        if (f>=1) {
            PageHelper.startPage(pn, 5);
            List<Contract> list= contractService.getstatus();
            PageInfo<Contract> page=new PageInfo<>(list,5);
            model.addAttribute("pageinfo",page);
        }else {
            return "error";
        }
        return "contract";
    }

}
